package com.lx.bookstore.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lx.bookstore.entity.Book;
import com.lx.bookstore.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/pageList")
    public String bookList(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize,"id desc");
        List<Book> list=bookService.bookList();
        PageInfo<Book> pageInfo=PageInfo.of(list);
        model.addAttribute("books",pageInfo);
        return "bookList";
    }

    @GetMapping("/goAdd")
    public String goAdd(){
        return "addBook";
    }
    @PostMapping("/addBook")
    public String addBook(Book book){
        book.setSales(0);
        boolean add= bookService.addBook(book);
        return "redirect:pageList";
    }

    @GetMapping("/delBook")
    public String delBook(Integer[] id){
        boolean del=bookService.delBook(id);
        return "redirect:pageList";
    }

    @GetMapping("/goUpdate")
    public String goUpdate(Integer id,Model model){
        Book book=bookService.all(id);
        model.addAttribute("id",id);
        model.addAttribute("all",book);
        return "update";
    }
    @PostMapping("/update")
    public String update(Book book){
        boolean up=bookService.update(book);
        return "redirect:pageList";
    }
}
